package com.ai.bitcoin.jpa.dao;

import com.ai.bitcoin.jpa.entity.ExtraRedeemCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ExtraRedeemCodeDao extends JpaRepository<ExtraRedeemCode, String> {

    List<ExtraRedeemCode> findByUserOpenIdAndAndIsActive(String userOpenId, Integer isActive);

    /**
     * 获取一个进行中的未领取的兑换码
     */
    @Query(nativeQuery = true,value = "update extra_redeem_code set state=1,update_time=now(), user_open_id = ?1 where id = ?2 and state=0 and user_open_id=''")
    @Modifying
    int updateOne(String userOpenId,String id);

    @Query(nativeQuery = true,value = "select id,code,is_active,state,create_time,update_time,remark,user_open_id from extra_redeem_code where is_active = 1 AND state = 0 AND user_open_id = '' limit 1")
    ExtraRedeemCode getOneActive();
}
